/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package seGobangClient.onlineHall;

import WebServiceLoginClient.LoginClient;
import seGobangClient.GlobalController;
import seGobangClient.ICommand;
import seGobangClient.IMessageBox;
import seGobangClient.gaming.*;

/**
 *
 * @author Administrator
 */
public class OnlineHallCommandsFactory
{
    /*由UI发起的事件*/

    class CommandTryLogin implements ICommand
    {

        private Server Receiver;

        public CommandTryLogin(Server Receiver)
        {
            this.Receiver = Receiver;
        }

        public Object execute(Object Param)
        {
            String[] info = (String[]) Param;
            Receiver.Login(info[0], info[1]);
            return null;
        }

        public Object undo()
        {
            throw new UnsupportedOperationException("Not supported yet.");
        }
    }

    ICommand createTryLoginCommand(Server server)
    {
        return new CommandTryLogin(server);
    }

    class CommandGetReady implements ICommand
    {

        private Server Receiver;

        public CommandGetReady(Server Receiver)
        {
            this.Receiver = Receiver;
        }

        public Object execute(Object Param)
        {
            Receiver.getReady();
            return null;
        }

        public Object undo()
        {
            throw new UnsupportedOperationException("Not supported yet.");
        }
    }

    ICommand createGetReadyCommand(Server server)
    {
        return new CommandGetReady(server);
    }

    class CommandCancelReady implements ICommand
    {

        private Server Receiver;

        public CommandCancelReady(Server Receiver)
        {
            this.Receiver = Receiver;
        }

        public Object execute(Object Param)
        {
            Receiver.cancelReady();
            return null;
        }

        public Object undo()
        {
            throw new UnsupportedOperationException("Not supported yet.");
        }
    }

    ICommand createCancelReadyCommand(Server server)
    {
        return new CommandCancelReady(server);
    }

    class CommandUpdateRoomList implements ICommand
    {

        private Server Receiver;

        public CommandUpdateRoomList(Server Receiver)
        {
            this.Receiver = Receiver;
        }

        public Object execute(Object Param)
        {
            Receiver.LoadRooms();
            return null;
        }

        public Object undo()
        {
            throw new UnsupportedOperationException("Not supported yet.");
        }
    }

    ICommand createUpdateRoomListCommand(Server server)
    {
        return new CommandUpdateRoomList(server);
    }

    class CommandJoinRoom implements ICommand
    {

        private Server Receiver;

        public CommandJoinRoom(Server Receiver)
        {
            this.Receiver = Receiver;
        }

        public Object execute(Object Param)
        {
            Receiver.joinRoom((Integer) Param);
            return null;
        }

        public Object undo()
        {
            throw new UnsupportedOperationException("Not supported yet.");
        }
    }

    ICommand createJoinRoomCommand(Server server)
    {
        return new CommandJoinRoom(server);
    }

    class CommandExitRoom implements ICommand
    {

        private Server Receiver;

        public CommandExitRoom(Server Receiver)
        {
            this.Receiver = Receiver;
        }

        public Object execute(Object Param)
        {
            Receiver.exitRoom();
            return null;
        }

        public Object undo()
        {
            throw new UnsupportedOperationException("Not supported yet.");
        }
    }

    ICommand createExitRoomCommand(Server server)
    {
        return new CommandExitRoom(server);
    }

    class CommandJoinDesk implements ICommand
    {

        private Server Receiver;

        public CommandJoinDesk(Server Receiver)
        {
            this.Receiver = Receiver;
        }

        public Object execute(Object Param)
        {
            Receiver.joinDesk((Integer) Param);
            return null;
        }

        public Object undo()
        {
            throw new UnsupportedOperationException("Not supported yet.");
        }
    }

    ICommand createJoinDeskCommand(Server server)
    {
        return new CommandJoinDesk(server);
    }

    class CommandExitDesk implements ICommand
    {

        private Server Receiver;

        public CommandExitDesk(Server Receiver)
        {
            this.Receiver = Receiver;
        }

        public Object execute(Object Param)
        {
            Receiver.exitDesk();
            return null;
        }

        public Object undo()
        {
            throw new UnsupportedOperationException("Not supported yet.");
        }
    }

    ICommand createExitDeskCommand(Server server)
    {
        return new CommandExitDesk(server);
    }

    class CommandExitHall implements ICommand
    {

        private IMessageBox Receiver;

        public CommandExitHall(IMessageBox Receiver)
        {
            this.Receiver = Receiver;
        }

        public Object execute(Object Param)
        {
            Receiver.showMessageBox(
                    IMessageBox.TYPE_QUESTION, "离开思弈棋舍",
                    "<html>离开思弈棋舍并返回主菜单!<br>确定吗？</html>",
                    "HaltHall");
            return null;
        }

        public Object undo()
        {
            throw new UnsupportedOperationException("Not supported yet.");
        }
    }

    ICommand createExitHallCommand(IMessageBox scene)
    {
        return new CommandExitHall(scene);
    }

    class CommandHaltHall implements ICommand
    {
        //此命令由ExitHall间接调用
        private OnlineHallController Receiver;

        public CommandHaltHall(OnlineHallController Receiver)
        {
            this.Receiver = Receiver;
        }

        public Object execute(Object Param)
        {
            int res = (Integer) Param;
            if (res == 1)
            {
                GlobalController.setCurrentSceneController(
                        new seGobangClient.mainMenu.MainMenuController(GlobalController.getCurrentUISystem()));
            }
            if (Receiver.UserName != null)
            {
                LoginClient loginClient = new LoginClient();
                int logoutMsg = loginClient.logout(Receiver.UserName + "");
                System.out.println("Logout message: " + logoutMsg);
            }
            return null;
        }

        public Object undo()
        {
            throw new UnsupportedOperationException("Not supported yet.");
        }
    }

    ICommand createHaltHallCommand(OnlineHallController ctrl)
    {
        return new CommandHaltHall(ctrl);
    }

    /*由Server发起的事件*/
    class CommandLoginView implements ICommand
    {

        IOnlineHallScene Receiver;

        public CommandLoginView(IOnlineHallScene scene)
        {
            Receiver = scene;
        }

        public Object execute(Object Param)
        {
            Receiver.changeToLogin();
            return null;
        }

        public Object undo()
        {
            throw new UnsupportedOperationException("Not supported yet.");
        }
    }

    ICommand createLoginViewCommand(IOnlineHallScene scene)
    {
        return new CommandLoginView(scene);
    }

    class CommandServerNotAvailable implements ICommand
    {

        private IMessageBox Receiver;

        public CommandServerNotAvailable(IMessageBox Receiver)
        {
            this.Receiver = Receiver;
        }

        public Object execute(Object Param)
        {
            Receiver.showMessageBox(
                    IMessageBox.TYPE_INFO, "无法连接",
                    "<html>由于网络异常,无法连接服务器!<br>请您稍候再试。我们对此深表歉意！</html>",
                    "HaltHall");
            return null;
        }

        public Object undo()
        {
            throw new UnsupportedOperationException("Not supported yet.");
        }
    }

    ICommand createServerNotAvailableCommand(IMessageBox scene)
    {
        return new CommandServerNotAvailable(scene);
    }

    class CommandRoomListView implements ICommand
    {
        //此命令由ExitHall间接调用
        IOnlineHallScene Receiver;

        public CommandRoomListView(IOnlineHallScene scene)
        {
            Receiver = scene;
        }

        public Object execute(Object Param)
        {
            Receiver.changeToRoomList();
            return null;
        }

        public Object undo()
        {
            throw new UnsupportedOperationException("Not supported yet.");
        }
    }

    ICommand createRoomListViewCommand(IOnlineHallScene scene)
    {
        return new CommandRoomListView(scene);
    }

    class CommandLoginFailed implements ICommand
    {
        private IMessageBox Receiver;

        public CommandLoginFailed(IMessageBox Receiver)
        {
            this.Receiver = Receiver;
        }

        public Object execute(Object Param)
        {
            Receiver.showMessageBox(
                    IMessageBox.TYPE_INFO, "登陆失败",
                    "<html>用户名密码验证错误!<br>请更正后重试或注册思弈通行证。</html>",
                    null);
            return null;
        }

        public Object undo()
        {
            throw new UnsupportedOperationException("Not supported yet.");
        }
    }

    ICommand createLoginFailedCommand(IMessageBox scene)
    {
        return new CommandLoginFailed(scene);
    }

    class CommandRefreshRoomList implements ICommand
    {

        IOnlineHallScene Receiver;

        public CommandRefreshRoomList(IOnlineHallScene scene)
        {
            Receiver = scene;
        }

        public Object execute(Object Param)
        {
            Room[] rooms = (Room[]) Param;
            Receiver.refreshRoomList(rooms);
            return null;
        }

        public Object undo()
        {
            throw new UnsupportedOperationException("Not supported yet.");
        }
    }

    ICommand createRefreshRoomListCommand(IOnlineHallScene scene)
    {
        return new CommandRefreshRoomList(scene);
    }

    /*由Room发起的事件*/
    class CommandRoomView implements ICommand
    {

        IOnlineHallScene Receiver;

        public CommandRoomView(IOnlineHallScene scene)
        {
            Receiver = scene;
        }

        public Object execute(Object Param)
        {
            Receiver.changeToRoom((String) Param);
            return null;
        }

        public Object undo()
        {
            throw new UnsupportedOperationException("Not supported yet.");
        }
    }

    ICommand createRoomViewCommand(IOnlineHallScene scene)
    {
        return new CommandRoomView(scene);
    }

    class CommandRefreshDeskState implements ICommand
    {

        IOnlineHallScene Receiver;

        public CommandRefreshDeskState(IOnlineHallScene scene)
        {
            Receiver = scene;
        }

        public Object execute(Object Param)
        {
            Desk[] Deskes = (Desk[]) Param;
            Receiver.refreshDeskState(Deskes);
            return null;
        }

        public Object undo()
        {
            throw new UnsupportedOperationException("Not supported yet.");
        }
    }

    ICommand createRefreshDeskStateCommand(IOnlineHallScene scene)
    {
        return new CommandRefreshDeskState(scene);
    }

    /*由Desk发起的事件*/
    class CommandDeskView implements ICommand
    {

        IOnlineHallScene Receiver;

        public CommandDeskView(IOnlineHallScene scene)
        {
            Receiver = scene;
        }

        public Object execute(Object Param)
        {
            Receiver.changeToDesk((Integer) Param);
            return null;
        }

        public Object undo()
        {
            throw new UnsupportedOperationException("Not supported yet.");
        }
    }

    ICommand createDeskViewCommand(IOnlineHallScene scene)
    {
        return new CommandDeskView(scene);
    }

    class CommandRefreshRemotePlayerInfo implements ICommand
    {

        IOnlineHallScene Receiver;

        public CommandRefreshRemotePlayerInfo(IOnlineHallScene scene)
        {
            Receiver = scene;
        }

        public Object execute(Object Param)
        {
            //Desk [] Deskes=(Desk []) Param;
            Receiver.refreshPlayerInfo((PlayerInfo) Param);
            return null;
        }

        public Object undo()
        {
            throw new UnsupportedOperationException("Not supported yet.");
        }
    }

    ICommand createRefreshRemotePlayerInfoCommand(IOnlineHallScene scene)
    {
        return new CommandRefreshRemotePlayerInfo(scene);
    }

    class CommandStartGame implements ICommand
    {
        OnlineHallController ctrl;
        public CommandStartGame(OnlineHallController Controller){
            this.ctrl=Controller;
        }

        public Object execute(Object Param)
        {
            Desk Desk = (Desk) Param;
            IPlayer player1, player2;
            if (Desk.playerLocal.ChessColor==-1){
                player1=new OnlineLocalPlayer(Desk.playerLocal.Name,Desk.playerLocal.ChessColor,
                        Desk.MyRoom.MyRule,Desk.MyRoom.RoomID,Desk.DeskID);
                player2=new OnlineRemotePlayer(Desk.playerRemote.Name,Desk.playerRemote.ChessColor,
                        Desk.MyRoom.MyRule,Desk.MyRoom.RoomID,Desk.DeskID);
            }else{
                player2=new OnlineLocalPlayer(Desk.playerLocal.Name,Desk.playerLocal.ChessColor,
                        Desk.MyRoom.MyRule,Desk.MyRoom.RoomID,Desk.DeskID);
                player1=new OnlineRemotePlayer(Desk.playerRemote.Name,Desk.playerRemote.ChessColor,
                        Desk.MyRoom.MyRule,Desk.MyRoom.RoomID,Desk.DeskID);
            }
            
            this.ctrl.pauseController();
            
            GlobalController.setCurrentSceneController(
                        new seGobangClient.gaming.GamingController(
                            GlobalController.getCurrentUISystem(),
                            player1, player2, this.ctrl
                            ));
            return null;
        }

        public Object undo()
        {
            throw new UnsupportedOperationException("Not supported yet.");
        }
    }

    ICommand createStartGameCommand(OnlineHallController Controller)
    {
        return new CommandStartGame(Controller);
    }
}
